package com.tandn.helpers;

import java.util.List;
import java.util.Vector;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

import com.tandn.models.ATM;
import com.tandn.models.Bank;

public class XMLParserHandler extends DefaultHandler
{
	/* The start TAG of XML for detect the Bank */
	public static final String XML_START = "bank";
	/* The TAG of XML for detect a ATM */
	public static final String XML_START_ATM = "atm";

	public static final String XML_START_ATTRIBUTE_NAME = "name";
	public static final String XML_START_ATTRIBUTE_SHORTCUT = "shortcut";

	public static final String XML_START_ATM_ATTRIBUTE_ID = "id";
	public static final String XML_START_ATM_ATTRIBUTE_NATION = "nation";
	public static final String XML_START_ATM_ATTRIBUTE_CITY = "city";
	public static final String XML_START_ATM_ATTRIBUTE_PLACE = "place";
	public static final String XML_START_ATM_ATTRIBUTE_DISTRICT = "district";

	/* The value of an element */
	// private String elementValue = null;
	/* The status OUT or On of an element */
	private boolean _isElementOn = false;
	private int _indexBank = -1;
	private int _indexATMInABank = -1;

	/* The list of ATMS */
	private List<Bank> _banksList = null;

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException
	{
		_isElementOn = true;
		if (localName.equalsIgnoreCase(XML_START))
		{
			Log.d("22222222", "read xml .. " + XML_START);
			if (_banksList == null)
				_banksList = new Vector<Bank>();
			_banksList.add(new Bank());
			_indexBank++;
			_indexATMInABank = -1;
			int numberAttribute = attributes.getLength();
			for (int i = 0; i < numberAttribute; i++)
			{
				String attributeName = attributes.getLocalName(i);
				String value = attributes.getValue(i);
				if (attributeName.equalsIgnoreCase(XML_START_ATTRIBUTE_NAME))
					_banksList.get(_indexBank).setName(value);
				else if (attributeName.equalsIgnoreCase(XML_START_ATTRIBUTE_SHORTCUT))
					_banksList.get(_indexBank).setShortcut(value);
			}
			_banksList.get(_indexBank).setATMsList(new Vector<ATM>());
		} else if (localName.equalsIgnoreCase(XML_START_ATM))
		{
			_banksList.get(_indexBank).getATMsList().add(new ATM());
			_indexATMInABank++;
			ATM tempATM = _banksList.get(_indexBank).getATMsList().get(_indexATMInABank);
			int numberAttribute = attributes.getLength();
			for (int i = 0; i < numberAttribute; i++)
			{
				String attributeName = attributes.getLocalName(i);
				String attributeValue = attributes.getValue(i);
				if (attributeName.equalsIgnoreCase(XML_START_ATM_ATTRIBUTE_CITY))
					tempATM.setCity(attributeValue);
				else if (attributeName.equalsIgnoreCase(XML_START_ATM_ATTRIBUTE_DISTRICT))
					tempATM.setDistrict(attributeValue);
				else if (attributeName.equalsIgnoreCase(XML_START_ATM_ATTRIBUTE_ID))
					tempATM.setId(attributeValue);
				else if (attributeName.equalsIgnoreCase(XML_START_ATM_ATTRIBUTE_NATION))
					tempATM.setNation(attributeValue);
				else if (attributeName.equalsIgnoreCase(XML_START_ATM_ATTRIBUTE_PLACE))
					tempATM.setPlace(attributeValue);

			}
		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException
	{
		if (_isElementOn)
		{
			// elementValue = new String(ch, start, length).trim();
			_isElementOn = false;
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException
	{
		_isElementOn = false;
	}

	/**
	 * @return a list banks
	 */
	public List<Bank> getBanksList()
	{
		return _banksList;
	}

	/**
	 * @return a specific list banks along attribute
	 */
	public List<String> getBanksListAttribute(String bankName,
			String attributeName)
	{
		if (_banksList == null || _banksList.size() == 0)
			return null;
		for (Bank bank : _banksList)
		{
			if (bank.getName().equalsIgnoreCase(bankName))
			{
				List<String> listStrings = new Vector<String>();
				for (ATM atm : bank.getATMsList())
				{
					if (attributeName.equalsIgnoreCase(XML_START_ATM_ATTRIBUTE_ID))
					{
						listStrings.add(atm.getId());

					} else if (attributeName.equalsIgnoreCase(XML_START_ATM_ATTRIBUTE_CITY))
					{
						listStrings.add(atm.getCity());
					} else if (attributeName.equalsIgnoreCase(XML_START_ATM_ATTRIBUTE_DISTRICT))
					{
						listStrings.add(atm.getDistrict());
					} else if (attributeName.equalsIgnoreCase(XML_START_ATM_ATTRIBUTE_NATION))
					{
						listStrings.add(atm.getNation());
					} else if (attributeName.equalsIgnoreCase(XML_START_ATM_ATTRIBUTE_PLACE))
					{
						listStrings.add(atm.getPlace());
					}
				}
				if(listStrings.size() == 0) return null;
				return listStrings;
			}

		}
		return null;
	}
}
